route.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { pick } from "lodash";
  2. import { NextResponse } from "next/server";
  3. import { WLEDClient } from "wled-client";
  4. import automation from "data/automation.json";
  5. const get = (url) => {
  6. // console.log("get", url);
  7. return fetch(url, { cache: "no-store" })
  8. .then((resp) => resp && resp.text())
  9. .then((result) => result);
  10. };
  11. const post = (url, value) => {
  12. // console.log("post", url, JSON.stringify(value));
  13. return fetch(url, {
  14. method: "POST",
  15. cache: "no-store",
  16. headers: {
  17. Accept: "application/json",
  18. "Content-Type": "application/json"
  19. },
  20. body: JSON.stringify(value)
  21. })
  22. .then((resp) => resp && resp.json())
  23. .then((result) => result);
  24. };
  25. // const update = async (client, { ps, ...value }) => {
  26. // console.log("update", client, JSON.stringify(value));
  27. // let url = `http://${client}/json/state`;
  28. // if (ps) {
  29. // await post(url, { ps: ps, v: true });
  30. // await sleep(1000);
  31. // }
  32. // let resp = await post(url, { ...value, v: true });
  33. // // console.log("->", "resp", resp);
  34. // let result = pick(resp, ["ps", ...Object.keys(value)]);
  35. // console.log("->", result);
  36. // return result;
  37. // };
  38. const update = ({ id, client, on, bri, ...rest }) => {
  39. let url = `http://${client}/win`;
  40. if (id) url += `&PL=${id}`;
  41. if (on) url += `&T=1`;
  42. else url += `&T=0`;
  43. if (bri) url += `&A=${bri}`;
  44. return get(url)
  45. .then((_) => ({
  46. ps: id,
  47. on: on,
  48. bri: bri
  49. }))
  50. .catch((err) => ({ error: err.message }));
  51. };
  52. const action = async ({ id, client, on, ...rest }) => {
  53. let wled = new WLEDClient(client); // setup a connection to the client
  54. // await wled.init(); // init the connection
  55. await wled.setPreset(id); // set the preset
  56. if (on) await wled.turnOn(); // turn off the lights
  57. else await wled.turnOff(); // turn off the lights
  58. await wled.refreshState();
  59. return {
  60. ...pick(wled?.state || {}, ["on", "brightness", "presetId"])
  61. };
  62. };
  63. export async function GET(req, { params }) {
  64. let promises = [];
  65. let id = params?.id || null;
  66. try {
  67. if (!id) throw new Error("No id specified");
  68. let clients = (automation?.[id] && Object.keys(automation?.[id])) || [];
  69. if (!clients) throw new Error("No clients fowr id", id);
  70. for (let client of clients) {
  71. promises.push(update({ id, client, ...automation?.[id]?.[client] }));
  72. }
  73. } catch (err) {
  74. return NextResponse.json({ error: err?.message }, { status: 500 });
  75. }
  76. return Promise.allSettled(promises)
  77. .then((results) => {
  78. return NextResponse.json(results?.map((o) => o?.value));
  79. })
  80. .catch((err) => {
  81. return NextResponse.json({ error: err?.message }, { status: 500 });
  82. });
  83. }